/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.integration;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.asportoonline.entities.Cliente;

/**
 *
 * @author fe
 */
@Stateless
public class ClienteFacadeImplementation extends AbstractFacade<Cliente> implements ClienteFacade {
    @PersistenceContext(unitName = "AsportoOnline-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ClienteFacadeImplementation() {
        super(Cliente.class);
    }

    public Cliente find(Long id) {
        Query query = em.createQuery("select c from Cliente c where c.idCliente = :idcliente");
        query.setParameter("idcliente", id);
        List<Cliente> lc = (List<Cliente>)query.getResultList();
        if(lc==null || lc.isEmpty())    return null;
        return (Cliente)lc.get(0);
    }

    public void persist(Cliente cliente) {
        em.persist(cliente);
    }
    
    
    
    public Cliente find(String email) {
        Query query = em.createQuery("select c from Cliente c where c.email = :email");
        query.setParameter("email", email);
        List<Cliente> lc = (List<Cliente>)query.getResultList();
        if(lc==null || lc.isEmpty())    return null;
        return (Cliente)lc.get(0);
    }

    @Override
    public void edit(Cliente cliente) {
        em.merge(cliente);
    }
    
}
